#!/bin/bash
set -xueo pipefail
export LANG=C LC_ALL=C

USR_PATH=/usr
VAR_PATH=/var

set_up() {
  rm -rf ./tests/unit/unity
  git submodule update --init ./tests/unit/unity

  rm -rf ./tests/unit/cmock
  git submodule update --init ./tests/unit/cmock

  if [ -e /usr/share/zookeeper/bin/zkServer.sh ]; then
    sudo /usr/share/zookeeper/bin/zkServer.sh restart
  elif [ -e /usr/lib/zookeeper/bin/zkServer.sh ]; then
    sudo /usr/lib/zookeeper/bin/zkServer.sh restart
  else
    echo "zkServer.sh command is not found"
    return 1
  fi
}

exec_unit() {
  ./autogen.sh
  ./configure --prefix=$USR_PATH --localstatedir=$VAR_PATH --enable-zookeeper --disable-corosync --enable-unittest
  make check -j $(nproc)
}

tear_down() {
  make clean
}

#main
set_up
exec_unit
tear_down
